//
//  WanTextView.m
//  MusicRunner
//
//  Created by Wan on 15/12/2.
//  Copyright © 2015年 WanWanSui. All rights reserved.
//

#import "THTextView.h"

@implementation THTextView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeColor" object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer {
    self = [super initWithFrame:frame textContainer:textContainer];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    self.backgroundColor = [UIColor whiteColor];
    self.textColor = [UIColor blackColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    if ([string isEqualToString:@"yes"]) {
        self.backgroundColor = [UIColor blackColor];
        self.textColor = [UIColor whiteColor];
    }
    
}

- (void)changeColor:(NSNotification *)notification {
    NSString *string = [notification object];
    if ([string isEqualToString:@"yes"]) {
        self.backgroundColor = [UIColor blackColor];
        self.textColor = [UIColor whiteColor];
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.textColor = [UIColor blackColor];
    }
    //保存当前状态
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"night"];
}


@end
